﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/App.Master" Inherits="System.Web.Mvc.ViewPage" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    创建用户
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <div class="buttonContainer">
        <a id="saveButton">创建</a> <a id="backButton">取消</a>
    </div>
    <div id="CreateUser">
        <ul>
            <li><a href="#base">必填项目</a> </li>
            <li><a href="#choose">选填项目</a> </li>
            <li><a href="#2">角色</a></li>
        </ul>
        <div id="base" class="cssform">
            <div class="fieldItem">
                <label for="LoginId">
                    登录名称:
                </label>
                <input id="LoginId" type="text" />

                <script type="text/javascript">
                    $("#LoginId").vRequire({
                        errorText: "请填写登录名",
                        group: "base"
                    }).vLength({
                        min: 6,
                        max: 20
                    }).vRegex({
                        pattern: /[a-zA-z1-9_-]{6,20}/,
                        errorText: "必须为应为字符、数字、下划线、减号、并且区分大小写"
                    }).vAjax({
                        errorText: "登录名称已被其他人使用，请修改其他",
                        url: '<%=Url.Action("IsExit")%>',
                        data: function() {
                            return { userName: $("#LoginId").val() };
                        },
                        onEndRequest: function(result) { return !result.Result; }
                    });        
                </script>

            </div>
            <p>
                <label for="EmailTextBox">
                    E-Mail:
                </label>
                <input id="EmailTextBox" type="text" />

                <script type="text/javascript">

                    $("#EmailTextBox").vRequire({
                        errorText: "请填写地址邮件地址",
                        group: "base"
                    }).vLength({
                        min: 6, max: 30, onerror: "电子邮件长度要在6只30之间"
                    }).vRegex({
                        pattern: /^([A-Za-z0-9_\.-]+)@([\da-zA-Z\.-]+)\.([a-zA-Z\.]{2,6})$/,
                        errorText: "电子邮件格式不正确"
                    });        
                </script>

            </p>
            <p>
                <label for="PasswordTextBox">
                    密码:
                </label>
                <input id="PasswordTextBox" type="password" />

                <script type="text/javascript">
                    $("#PasswordTextBox").vRequire({
                        errorText: "请输入至少" + passwordMinLength + "位密码",
                        group: "base"
                    }).vLength({
                        min: 6,
                        max: 20
                    }).vRegex({
                        pattern: /\w{6,20}/,
                        errorText: "请输入数值或英文字符"
                    });
                </script>

            </p>
            <p>
                <label for="ConfirmPasswordTextBox">
                    确认密码
                </label>
                <input id="ConfirmPasswordTextBox" type="password" />

                <script type="text/javascript">
                    $("#ConfirmPasswordTextBox").vCompare({ target: $("#PasswordTextBox"), errorText: "两次密码不一样" });
                </script>

            </p>
            <%if (Membership.RequiresQuestionAndAnswer)
              {%>
            <p>
                <label for="QuestionTextBox">
                    问题:
                </label>
                <input id="QuestionTextBox" type="text" />

                <script type="text/javascript">

                    $("#QuestionTextBox").vRequire({
                        errorText: "请填写取回密码的问题",
                        group:"base",
                    }).vLength({ min: 1, max: 50, errorText: "长度必须在1至50之间" });
                </script>

            </p>
            <p>
                <label for="AnswerTextBox">
                    答案:
                </label>
                <input id="AnswerTextBox" type="text" />

                <script type="text/javascript">
                    $("#AnswerTextBox").vRequire({
                        errorText: "请填写取回密码的答案",
                        group: "base"
                    }).vLength({ min: 1, max: 50, errorText: "长度必须在1至50之间" });
                </script>

            </p>
            <%}%>
        </div>
        <div id="choose" class="cssform">
            <p>
                <label for="NameTextBox">
                    名称:
                </label>
                <input id="NameTextBox" type="text" />

                <script type="text/javascript">
                    $("#NameTextBox").vLength({ min: 1, max: 30, errorText: "长度必须在1至30之间" });
                </script>

            </p>
            <p>
                <label for="PhoneTextBox">
                    电话:
                </label>
                <input id="PhoneTextBox" type="text" />
                <span id="PhoneTextBoxTip"></span>
            </p>
            <p>
                <label for="CommentTextBox">
                    备注:
                </label>
                <textarea id="CommentTextBox" cols="10" rows="5"></textarea>
                <span id="CommentTextBoxTip"></span>
            </p>
        </div>
        <div id="2">
            <%Html.RenderPartial("../Role/RoleAssign"); %>
        </div>
    </div>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="HeadContent" runat="server">

    <script src="/Scripts/jQuery.easyValidate.js" type="text/javascript"></script>

    <script src="/Scripts/jquery.json.min.js" type="text/javascript"></script>

    <script src="/Scripts/jquery.bindTools.js" type="text/javascript"></script>

    <script language="javascript" type="text/javascript">
    
    var passwordMinLength=<%=Membership.MinRequiredPasswordLength%>;
    var requiresQuestionAndAnswer=<%=Membership.RequiresQuestionAndAnswer.ToString().ToLower()%>;   
    
        
    $(document).ready(function() {
        
        $('#CreateUser').tabs();
        var opts={ 
            bindWay:[
                {member:"LoginId"},
                {member:"Email",to:"#EmailTextBox"},
                {member:"Password",to:"#PasswordTextBox"},
                {member:"Name",to:"#NameTextBox"},
                {member:"Phone",to:"#PhoneTextBox"},                
                {member:"Comment",to:"#CommentTextBox"}                
            ]
        };
        
        
        if (requiresQuestionAndAnswer) {         
            opts.bindWay.push({ member:"Question",to:"#QuestionTextBox"});
            opts.bindWay.push({member:"Answer",to:"#AnswerTextBox"});        
        }
        
        $("#CreateUser").bindTool(opts);          
        $("#backButton").button().click(function(){      
            location="Index";
         });
        $("#saveButton").button({icons:{primary:"ui-icon-plus"} }).click(function(){        
        
          if(!$.easyValidate('check')){
            return false;
          }           
          
          var user=$("#CreateUser").bindTool('collect');
          var roles=GetAssignRole();
          for (var i = 0; i <roles.length; i++) {
            listItem = roles[i];
            user.Roles.push(listItem.value);
          }          
          
            $.post('<%=Url.Action("Create")%>',{user:$.toJSON(user)},function(data){
                if(data.Success){
                    if(confirm(data.Message+",继续创建下一个用户吗?")){
                        $("#CreateUser").bindTool("clear");
                    }
                    else{
                        location='<%=Url.Action("Index") %>';
                    }
                }
                else{
                    alert("创建失败,错误信是"+data.Message);
                }
            });
           
        });        
         
    });
  
    
    </script>

</asp:Content>
<asp:Content ID="Content4" ContentPlaceHolderID="PageTitle" runat="server">
    创建用户
</asp:Content>
